package com.hengda.museumonline.utils;

import android.content.Context;
import android.content.Intent;

import com.google.gson.Gson;
import com.hengda.museumonline.MyApplication;
import com.hengda.museumonline.entity.LoginBean;
import com.hengda.museumonline.entity.UinfoBean;
import com.hengda.museumonline.http.CallServer;
import com.hengda.museumonline.ui.LoginActivity;
import com.litesuits.common.assist.Base64;
import com.litesuits.common.utils.PackageUtil;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.OnResponseListener;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

/**
 * @author：JFWU on 2016/10/22 14:59
 * @E-mail：jeffy12138@126.com
 */
public class UinfoUtil {
    private static UinfoBean uinfoBean = new UinfoBean();
    private OnUinfoListener listener;

    public UinfoUtil(Context context, String token) {
        Request request = NoHttp.createStringRequest(MyApplication.url + "user/uinfo", RequestMethod.GET);
        request.add("p", "a");
        request.add("v", PackageUtil.getAppPackageInfo(context).versionName);
        request.add("token", token);
        CallServer.getRequestInstance().add(context, 0, request, new OnResponseListener() {
            @Override
            public void onStart(int what) {

            }

            @Override
            public void onSucceed(int what, Response response) {
                System.out.println("response:" + response.get().toString());
                if (200 == response.getHeaders().getResponseCode()) {
                    Gson gson = new Gson();
                    uinfoBean = gson.fromJson(response.get().toString(), UinfoBean.class);
                    listener.onUinfo(uinfoBean);
                }

            }

            @Override
            public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
                System.out.println("onFailed:" + exception);
            }

            @Override
            public void onFinish(int what) {
            }
        });
    }

    public void setOnUinfoListener(OnUinfoListener listener) {
        this.listener = listener;
    }

    public interface OnUinfoListener {
        void onUinfo(UinfoBean uinfoBean);
    }
}
